package prototyping.client;

import java.io.Serializable;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.Date;

public abstract class AbstractMessage implements Serializable, Comparable<AbstractMessage>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected String message;
	protected String user;
	protected String ipAdress;
	protected long timeStamp;
	protected boolean isDone = false;

	public AbstractMessage() throws UnknownHostException {
		this("Miscalleneous");
	}

	public AbstractMessage(String message) throws UnknownHostException{
		this(message, Inet4Address.getLocalHost().getHostName());
	}

	public AbstractMessage(String message, String user) throws UnknownHostException {
		this(message, user, Inet4Address.getLocalHost().getHostAddress());
	}

	public AbstractMessage(String message, String user, String ipAdress) {
		super();
		this.message = message;
		this.user = user;
		this.ipAdress = ipAdress;
		Date d = new Date();
		timeStamp = d.getTime();
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getIpAdress() {
		return ipAdress;
	}

	public void setIpAdress(String ipAdress) {
		this.ipAdress = ipAdress;
	}

	@Override
	public String toString() {
		return message;
	}

	@Override
	public int hashCode() {
		return (int)timeStamp;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractMessage other = (AbstractMessage) obj;
		if(other.getTimeStamp() == this.timeStamp){
			return true;
		}
		return false;
	}

	public long getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(long timeStamp) {
		this.timeStamp = timeStamp;
	}

	@Override
	public int compareTo(AbstractMessage o) {
		if(timeStamp > o.getTimeStamp()){
			return -1;
		}
		else if(timeStamp < o.getTimeStamp()){
			return 1;
		}
		return 0;
	}

	public boolean isDone() {
		return isDone;
	}

	public void setDone(boolean isDone) {
		this.isDone = isDone;
	}

}
